<?php
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;

class HomeController extends BaseController
{
    
    /*
     * |--------------------------------------------------------------------------
     * | Default Home Controller
     * |--------------------------------------------------------------------------
     * |
     * | You may wish to use controllers instead of, or in addition to, Closure
     * | based routes. That's great! Here is an example controller method to
     * | get you started. To route to this controller, just add the route:
     * |
     * |	Route::get('/', 'HomeController@showWelcome');
     * |
     */
    public function showWelcome($view = 'hello')
    {
        if (Auth::check()) {
            if($view == 'login')
                return Redirect::to('hello');
            $view = explode('-', $view)[0];
            return View::make($view);//Redirect::to($view);
        } else {
            return Redirect::to('login');
        }
        //return View::make('hello');
    }

    public function login()
    {
        if (Auth::check()) {
            return Redirect::to('/');
        }
        $username = Input::get('username', null);
        $userpassword = Input::get('userpassword', null);
        if (is_null($username)) {
            return View::make('login');
        } else {
            $user = User::where('username', '=', $username)->where('password', '=', md5($userpassword))->first();
            if (is_null($user)) {
                return View::make('login');
            }
            Auth::login($user);
            return Redirect::to('/');
        }
    }

    public function logout()
    {
        Auth::logout();
        return Redirect::to('login');
    }
}

